var app = angular.module("myApp",["globalApp"]);

app.controller("myCtrl",function($scope,$http,getPostfee){
	$scope.reminder = false;
	$scope.close = function(){
		$scope.reminder = false;
	}                      //提示框
	$scope.show = false;   //存在默认地址，隐藏
	$scope.hide = true;    //存在默认地址，出现
	//请求收货人地址
	$http({
        method : "post",
        dataType: "json",
		withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
	        
	    crossDomain: true,
	    contentType: "application/x-www-form-urlencoded",//表单提交
        async:true,
        url : host + "/terminal/shipping/getDefault"
   }).success(function(response){
        if(response.code == 2000){
        	if(response.data.shipping){
        		$scope.show = false;
        		$scope.hide = true;
        		$scope.address = response.data.shipping;
        		$scope.addressCode = response.data.shipping.code;       //获取默认收货地址的code
        		console.log($scope.addressCode);
        	}else{
        		$scope.show = true;
        		$scope.hide = false;
        	}
        }else{
        	$scope.show = true;
        	$scope.hide = false;
        }
    });
    
    //跳转到我的地址
    $scope.checkAddress = function(){
    	window.location.href = "my_address.html";
    }
    
    $scope.goodsArr = [];  //存放从购物车传来的数组
    $scope.arr = [];       //确认是哪一条购物单的数组
    //操作，将sessionStorage转为数组
    if(window.sessionStorage.goods){
    	if(window.sessionStorage.goods.indexOf(",") > 0){
    		$scope.goodsArr = window.sessionStorage.goods.split(",");
    	}else{
    		$scope.goodsArr[0] = window.sessionStorage.goods;
    	}
    }
    $scope.goodsNum = $scope.goodsArr.length;
    //请求购物车的数据
    var params = {
	    pageSize : 20
	};
	params = $.param(params);
    $http({
        method : "get",
        dataType: "json",
		withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
	    crossDomain: true,
	    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
        data : params,
        async:false,
        url : host + "/terminal/shoppingCart/list"
    }).success(function(response){
        if(response.code == 2000){
        	console.log(response);
        	for(var i=0; i<$scope.goodsArr.length; i++){
        		for(var j=0; j<response.data.shoppingCarts.length; j++){
        			if($scope.goodsArr[i] == response.data.shoppingCarts[j].code){
        				$scope.arr.push(response.data.shoppingCarts[j]);
        			}
        		}
        	}
        	console.log($scope.arr);
        	var str;
        	$scope.goodsData = [];
        	for(var i=0; i<$scope.arr.length; i++){
		    	str = {
		    		"goodsCode" : $scope.arr[i].goods.code,
		    		"num" : $scope.arr[i].num 
		    	}
		    	$scope.goodsData.push(str);
		    }
        	$scope.goodsData = JSON.stringify($scope.goodsData)
        	console.log($scope.goodsData);

            //(根据已选中商品)获得订单邮费与应付款
            var params = {
                goodsJson : $scope.goodsData
            };
            params = $.param(params);
            getPostfee.initPostfee(params,function(x){
                console.log(x);
                $scope.money = x.data.account;
                window.sessionStorage.totalPrice = $scope.money.postFee + $scope.money.payment;
            });
        }else{
        	
        }
    });
    
    //(根据已选中商品)获得订单邮费与应付款
    /*var params = {
            goodsJson : $scope.goodsData
        };
    params = $.param(params);
    $http({
        method : "post",
        dataType: "json",
		withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
	    data : params,
	    crossDomain: true,
	    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
        async:false,
        url : host + "/terminal/orderItem/account"
   }).success(function(response){
        if(response.code == 2000){
            console.log(response);
        	$scope.money = response.data.account;
        	window.sessionStorage.totalPrice = $scope.money.postFee + $scope.money.payment;
        }else{
        	
        }
    });*/
    
    //点击结算
    $scope.account = function(){
    	if($scope.show == true){
    		$scope.reminder = true;
    		$scope.message = "地址不能为空";
    		return;
    	}
    	//提交订单获取orderCode
    	$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    data : {
	    		goodsJson : $scope.goodsData,
			    orderShippingCode : $scope.addressCode
	    	},
		    crossDomain: true,
		    contentType: "application/x-www-form-urlencoded",//表单提交
	        //async:false,
	        url : host + "/terminal/orderItem/add"
	   }).success(function(response){
	        if(response.code == 2000){
	        	console.log(response);
	        	window.sessionStorage.orderCode = response.data.orderCode;
	        	location.href = "paymentOrder.html";
	        }else{
	        	
	        }
	    });
    }
    
})

app.factory("getPostfee",function($http){
    var obj = {};

    obj.initPostfee = function(params,_callback){
        $http({
            method : "post",
            dataType: "json",
            withCredentials: true,  //跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
            data : params,
            crossDomain: true,
            headers:{'Content-Type': 'application/x-www-form-urlencoded'},
            async:false,
            url : host + "/terminal/orderItem/account"
       }).success(function(response){
            if(response.code == 2000){
                _callback(response);
            }else{
                
            }
        });
    }

    return obj;
})